import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
import localCache from '../utils/cache'
import { firstRoute } from "@/utils/routeToMenu";
const routes:Array<RouteRecordRaw> = [
  {
    path: '',
    redirect:'/login'
  },
  {
    path: '/login',
    name:'login',
    component:() => import('../views/login/Login.vue')
  },
  {
    path: '/main',
    name:'main',
    component:() => import('../views/main/Main.vue')
  },
  {
    path: '/:pathMatch(.*)*',
    name:'not-found',
    component:() => import('../views/not-found/NotFound.vue')
  }
]
const router = createRouter({
  routes,
  history:createWebHashHistory()
})
router.beforeEach(to => {
  const token = localCache.getCache('token')
  
  if (token) {
    if (to.path === '/login' || to.path === '/main') {
      return firstRoute.url
    }
  } else {
    if (to.path.indexOf('/main') !== -1) {
      return '/login'
    }
  }
})

export default router